package com.sale.succ.forms;

import com.sale.succ.components.DefButton;
import com.sale.succ.components.DefForm;
import com.sale.succ.utils.TraderConstants;
import com.sale.succ.utils.TraderHelper;

import eve.fx.Insets;
import eve.fx.gui.WindowConstants;
import eve.ui.CellPanel;
import eve.ui.Label;

public class ChoiceForm extends DefForm {
	String clientName;

	public ChoiceForm(String client) {
		clientName = client;
		// fullScreenOnPDA();
		title = TraderHelper.getLabel(TraderConstants.CHOICE_TITLE);
		addLast(getMainPanel());
		windowFlagsToSet |= WindowConstants.FLAG_MAXIMIZE;
	}

	private CellPanel mainPanel;

	protected CellPanel getMainPanel() {
		if (mainPanel == null) {
			mainPanel = new CellPanel();
			mainPanel.defaultTags.set(TAG_INSETS, new Insets(5, 5, 5, 5));

			Label helloLabel = new Label(TraderHelper.getLabel(TraderConstants.CHOICE_TITLE) + ":");
			clientName = clientName.length()>30? clientName.substring(0,30):clientName;
			Label clientLabel = new Label(clientName);
			clientLabel.setPreferredSize(200, 25);
			clientLabel.setMaximumSize(200, 25);
			mainPanel.addLast(helloLabel).setCell(HGROW).setControl(HCENTER);
			mainPanel.addLast(clientLabel).setCell(HGROW).setControl(HCENTER);
			mainPanel.addLast(getOrderButton()).setCell(HGROW).setControl(
					HCENTER);
			mainPanel.addLast(getHistoryButton()).setCell(HGROW).setControl(
					HCENTER);

		}
		return mainPanel;
	}

	private DefButton orderButton;
	private DefButton historyButton;

	public DefButton getHistoryButton() {
		if (historyButton == null) {
			historyButton = new DefButton(TraderHelper
					.getLabel(TraderConstants.HISTORY));
			historyButton.action = TraderConstants.HISTORY_ACTION;
			historyButton.setPreferredSize(110, 25);
		}
		return historyButton;
	}

	private DefButton getOrderButton() {
		if (orderButton == null) {
			orderButton = new DefButton(TraderHelper
					.getLabel(TraderConstants.ORDER));
			orderButton.action = TraderConstants.ORDER_ACTION;
			orderButton.setPreferredSize(60, 25);
		}
		return orderButton;
	}

	public boolean handleAction(String action) {
		if (action.equals(TraderConstants.HISTORY_ACTION)) {
			new OrdersForm().execute();
			return true;
		}
		if (action.equals(TraderConstants.ORDER_ACTION)) {
			new ClientPropertiesForm().execute();
			return true;
		}
		return super.handleAction(action);
	}

}
